package com.ficus.road.maintain.core.interceptor;

import com.ficus.road.maintain.core.exception.InternalServerException;
import com.ficus.road.maintain.core.exception.InvalidRequestException;
import com.ficus.road.maintain.core.model.common.ErrorCodeDTO;
import com.ficus.road.maintain.core.model.common.ErrorDTO;
import io.grpc.netty.shaded.io.netty.handler.codec.http2.Http2Exception;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.ClientAbortException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: fip-main
 * @author: dwang
 * @create: 2019-06-15 17:03
 * @description: GlobalExceptionHandler
 **/

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    private static final String ERROR_CLAUSE = "Error to handle request: {}.";

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseEntity<ErrorDTO> serviceExceptionHandler(HttpServletRequest request, Exception e) {
        if (e instanceof Http2Exception.StreamException || e instanceof ClientAbortException) {
            log.error("client error, error request: {}", request.getRequestURI());
            return new ResponseEntity<>(ErrorDTO.builder().message(e.getMessage()).retryable(false).code(ErrorCodeDTO.UNIMPLEMENTED_OPERATION).build(), HttpStatus.EXPECTATION_FAILED);
        }
        log.warn(ERROR_CLAUSE, request.getRequestURI(), e);
        if (e instanceof InvalidRequestException) {
            InvalidRequestException se = (InvalidRequestException) e;
            ErrorDTO errorDTO = se.getErrorDTO();
            return new ResponseEntity<>(errorDTO, HttpStatus.BAD_REQUEST);
        } else if (e instanceof InternalServerException) {
            InternalServerException se = (InternalServerException) e;
            ErrorDTO errorDTO = se.getErrorDTO();
            return new ResponseEntity<>(errorDTO, HttpStatus.INTERNAL_SERVER_ERROR);
        } else if (e instanceof MaxUploadSizeExceededException) {
            ErrorDTO errorDTO = new ErrorDTO();
            errorDTO.setMessage("File is too　big! please compress your file!");
            errorDTO.setCode(ErrorCodeDTO.INVALID_PARAMETER);
            errorDTO.setRetryable(false);
            return new ResponseEntity<>(errorDTO, HttpStatus.PAYLOAD_TOO_LARGE);
        }
        return new ResponseEntity<>(ErrorDTO.builder().message(e.getMessage()).retryable(false).code(ErrorCodeDTO.SERVER_BUSY).build(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

//    @ExceptionHandler(AccessDeniedException.class)
//    public void accessDeniedException(AccessDeniedException e) throws AccessDeniedException {
//        throw e;
//    }

//    @ExceptionHandler(UnAuthorizedException.class)
//    public ResponseEntity<String> handleUnauthorized(UnAuthorizedException e) {
//        return new ResponseEntity<>(e.getMessage(), e.getStatus());
//    }
//
//    @ExceptionHandler(BadRequestException.class)
//    public ResponseEntity<ErrorDTO> handleBadRequest(BadRequestException e) {
//        ErrorDTO errorDTO = new ErrorDTO();
//        errorDTO.setMessage(e.getMessage());
//        errorDTO.setCode(ErrorCodeDTO.INVALID_PARAMETER);
//        errorDTO.setRetryable(false);
//        return new ResponseEntity<>(errorDTO, e.getStatus());
//    }
}
